package com.bwie.system.service.impl;

import com.bwie.common.constants.Constants;
import com.bwie.common.domain.Goods;
import com.bwie.common.domain.Orders;
import com.bwie.common.exceptions.BusinessException;
import com.bwie.system.mapper.GoodsMapper;
import com.bwie.system.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public PageInfo<Goods> goodsList(Goods goods) {
        PageHelper.startPage(goods.getPageNum(),goods.getPageSize());
        List<Goods> list = goodsMapper.goodsList(goods);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Transactional
    @Override
    public void addGoods(List<Integer> goodsIds) {
        if(goodsIds==null){
            throw new BusinessException("参数异常");
        }
       List<Goods> goodsList = goodsMapper.findPrice(goodsIds);
        if(goodsList==null){
            throw new BusinessException("列表返回为空");
        }
        BigDecimal bigDecimal = new BigDecimal(0);
        for (Goods goods : goodsList) {
            BigDecimal goodsPrice = goods.getGoodsPrice();
            bigDecimal.add(goodsPrice);
        }
        //获取到总价格，保存带数据库
        goodsMapper.addPrice(bigDecimal);
        goodsMapper.addOrderBusin(goodsIds);
        redisTemplate.delete(Constants.ORDERS_LIST);
    }
}
